﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Configuration;
using System.Text;
using System.Xml;


namespace moose {
    /// <summary>
    /// Global Settings
    /// </summary>
    [CLSCompliant(true)]
    public class Settings {

        #region [Local Attributes]

        /// <summary>
        /// Our Settings Xml Document
        /// </summary>
        private static XmlDocument m_settings_doc;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Static Members
        /// </summary>
        static Settings() {
            m_settings_doc = new XmlDocument();
            m_settings_doc.Load(Environment.CurrentDirectory 
                + "/" + ConfigurationManager.AppSettings["conf"]);
        }

        #endregion

        #region [Public Properties]

        /// <summary>
        /// Gets default encoding 
        /// </summary>
        public static Encoding DEFAULT_ENCODING {
            get {
                return Encoding.UTF8;
            }
        }

        #endregion

        #region [Public Methods]

        /// <summary>
        /// Get a setting by xpath
        /// </summary>
        /// <param name="xpath">xpath to our setting</param>
        /// <returns>setting value, or null if does not exist</returns>
        public static string get(string xpath) {
            XmlNode nod = m_settings_doc.SelectSingleNode(xpath);
            if (nod != null)
                return nod.Value;
            else
                return null;
        }

        /// <summary>
        /// Set a new value to an existing setting,
        /// </summary>
        /// <param name="xpath">xpath of setting to modify</param>
        /// <param name="value">setting value</param>
        public static void set(string xpath, string value) {
            //I dont really care right now about creating new nodes, for now, I'll only use paths I know exist
            XmlNode nod = m_settings_doc.SelectSingleNode(xpath);
            if (nod != null) {
                nod.Value = value;
            }
            return;
        }

        /// <summary>
        /// Save Settings
        /// </summary>
        public void save() {
            m_settings_doc.Save(Environment.CurrentDirectory
                + "/" + ConfigurationManager.AppSettings["conf"]);
            return;
        }

        #endregion

    }
}
